package cn.wolfcode.web.controller;


import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.domain.MessageReply;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageReplyService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/message")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;

    @Autowired
    private IMessageReplyService messageReplyService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        List<SystemDictionaryItem> items = systemDictionaryItemService.selectByParentId(1L);
        model.addAttribute("items",items);
        return "message/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            messageBoardService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(MessageBoard messageBoard){
        messageBoard.setCreateTime(new Date());
        messageBoard.setReplystatus(false);
        if (messageBoard.getId() != null) {
            messageBoardService.update(messageBoard);
        }else {
            messageBoardService.save(messageBoard);
        }
        return new JsonResult();
    }

    //查看详细
    @RequestMapping("/details")
    public String details(Model model,Long id){
        List<MessageReply> messageReplies = messageReplyService.selectByMessageId(id);
        model.addAttribute("messageReplies",messageReplies);
        MessageBoard messageBoard = messageBoardService.get(id);
        model.addAttribute("messageBoard",messageBoard);
        return "message/details";
    }
}
